﻿using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Common.Utilities.Excel.Validator
{
    /// <remarks>
    /// 验证功能类
    /// </remarks>
    public class ValidatorUtilites
    {
        private const string KEY_VALUE_STRING = "value";
        private const string KEY_LABEL_STRING = "label";
        private const string KEY_VALIDATOR_STRING = "validator";

        /// <summary>
        /// 验证配置数据列表的数据是否正确
        /// </summary>
        /// <param name="list"></param>
        /// <returns>验证结果对象，包括是否正确和错误信息</returns>
        public static ValidatorResult Validator(IList<ValidatorConfig> list)
        {
            ValidatorResult result = new ValidatorResult();

            foreach (ValidatorConfig config in list)
            {
                result.AddValidatorResult(config.ValidatorData());
            }

            return result;
        }

        /// <summary>
        /// 验证一个配置数据的数据是否正确
        /// </summary>
        /// <param name="config"></param>
        /// <returns>验证结果对象，包括是否正确和错误信息</returns>
        public static ValidatorResult Validator(ValidatorConfig config)
        {
            return config.ValidatorData();
        }

        /// <summary>
        /// 验证一个服务端容器中所有包含了"validator"的服务器控件数据是否正确
        /// </summary>
        /// <param name="container">服务端容器</param>
        /// <returns>验证结果对象，包括是否正确和错误信息</returns>
        public static ValidatorResult ValidatorContainer(Control container)
        {
            ValidatorResult result = new ValidatorResult();

            foreach (Control ctrl in container.Controls)
            {
                ValidatorConfig config = BuildValidatorConfig(ctrl);
                if (!string.IsNullOrEmpty(config.Validator))
                {
                    result.AddValidatorResult(config.ValidatorData());
                }
            }

            return result;
        }

        /// <summary>
        /// 验证一个服务器控件数据是否正确
        /// </summary>
        /// <param name="control">服务端容器</param>
        /// <returns>验证结果对象，包括是否正确和错误信息</returns>
        public static ValidatorResult ValidatorControl(Control control)
        {
            ValidatorConfig config = BuildValidatorConfig(control);
            return config.ValidatorData();
        }

        /// <summary>
        /// 获取验证错误信息
        /// </summary>
        /// <param name="result"></param>
        /// <param name="splitString">分隔符</param>
        /// <returns>错误信息字符串</returns>
        public static string GetValidatorResultMessages(ValidatorResult result, string splitString)
        {
            StringBuilder str = new StringBuilder();
            foreach (string msg in result.MessageList)
            {
                str.Append(msg);
                str.Append(splitString);
            }

            // 除去多余的分隔符
            string rtnStr = str.ToString();
            if (rtnStr.Length > splitString.Length)
            {
                rtnStr = rtnStr.Substring(0, rtnStr.Length - splitString.Length);
            }

            return rtnStr;
        }

        /// <summary>
        /// 通过服务器控件构建配置数据对象
        /// </summary>
        /// <param name="ctrl"></param>
        /// <returns></returns>
        private static ValidatorConfig BuildValidatorConfig(Control ctrl)
        {
            // 获取控件值
            string value = "";
            string label = "";
            string validator = "";

            if (ctrl is HtmlInputControl)
            {
                value = ((HtmlInputControl)ctrl).Value;
                label = ((HtmlInputControl)ctrl).Attributes[KEY_LABEL_STRING];
                validator = ((HtmlInputControl)ctrl).Attributes[KEY_VALIDATOR_STRING];
            }
            else if (ctrl is HtmlTextArea)
            {
                value = ((HtmlTextArea)ctrl).Value;
                label = ((HtmlTextArea)ctrl).Attributes[KEY_LABEL_STRING];
                validator = ((HtmlTextArea)ctrl).Attributes[KEY_VALIDATOR_STRING];
            }
            else if (ctrl is TextBox)
            {
                value = ((TextBox)ctrl).Text;
                label = ((TextBox)ctrl).Attributes[KEY_LABEL_STRING];
                validator = ((TextBox)ctrl).Attributes[KEY_VALIDATOR_STRING];
            }
            else if (ctrl is DropDownList)
            {
                value = ((DropDownList)ctrl).SelectedValue;
                label = ((DropDownList)ctrl).Attributes[KEY_LABEL_STRING];
                validator = ((DropDownList)ctrl).Attributes[KEY_VALIDATOR_STRING];
            }
            else if (ctrl is ListControl)
            {
                value = ((ListControl)ctrl).SelectedValue;
                label = ((ListControl)ctrl).Attributes[KEY_LABEL_STRING];
                validator = ((DropDownList)ctrl).Attributes[KEY_VALIDATOR_STRING];
            }

            // 如果Label为空，则默认为控件ID
            if (string.IsNullOrEmpty(label))
            {
                label = ctrl.ID;
            }

            ValidatorConfig config = new ValidatorConfig();
            config.Value = value;
            config.Label = label;
            config.Validator = validator;

            return config;
        }
    }
}
